package com.ad.pet.common.structure.presenter.photo;

import android.content.Context;

import com.ad.pet.Logger;
import com.ad.pet.api.ApiService;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.injector.PerActivity;
import com.ad.pet.common.execption.utils.ExceptionHandle;

import java.io.File;

import javax.inject.Inject;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import top.zibin.luban.Luban;

/**
 * Created by z on.
 * Des:
 * Date: 2017/3/14
 */
@PerActivity
public class PhotoPresenter implements PhotoContract.Presenter {
    private  PhotoContract.View mView;
    private  Context mContext;
    private ApiService mApiService;
    private RxDisposable mRxDisposable;

    @Inject
    public PhotoPresenter(Context context, ApiService apiService,RxDisposable rxDisposable) {
        mContext = context;
        mApiService = apiService;
        mRxDisposable=rxDisposable;
    }

    @Override
    public void onDestroy() {
        if (mRxDisposable != null)
        mRxDisposable.clear();
        mView = null;
    }

    @Override
    public void onCreate(PhotoContract.View view) {
        this.mView = view;
    }

    @Override
    public void requestRetry() {

    }

    @Override
    public void compressPhoto(final File file) {
              if (file == null) {
        mView.error("文件为空");
        return;
    }
    if (!file.exists()) {
        mView.photoCallback(file.getPath());
        return;
    }
        final File tempFile = new File(file.getPath(), file.getName());
        mView.showLoading();
        mRxDisposable.add(Luban.get(mContext).load(file)
                .asObservable()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<File>() {
                    @Override
                    public void onCompleted() {
                        mView.hideLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        mView.hideLoading();
                        mView.compressComplete(tempFile);
                        Logger.e(ExceptionHandle.handle(e).getMessage());
                    }

                    @Override
                    public void onNext(File file) {
                        mView.compressComplete(file);
                    }
                }));
    }

    @Override
    public void upload(final File file,int type,String userId) {
//        mRxDisposable.add(mApiService.uploadImage(file, type,userId)
//                .subscribe(new Subscriber<ResultBean<Upload>>() {
//            @Override
//            public void onCompleted() {
//                mView.hideLoading();
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                e.printStackTrace();
//                mView.hideLoading();
//                mView.uploadError(ExceptionHandle.handle(e));
//            }
//
//            @Override
//            public void onNext(ResultBean<Upload> stringResultBean) {
//                mView.uploadComplete(stringResultBean.getData().getMedia_path());
//                mView.uploadCompleteThumb(stringResultBean.getData().getMediathumb_path());
//            }
//        }));
    }
}
